<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2020/06/09 17:02
 * Desc: 订单退订
 */
class Controller_Pc_Api_Standard_Refund extends Controller_Pc_Api_Base
{

    // 获取退款详情
    public function action_get_refund_info()
    {
        $orderid = intval($this->request_body->content->orderid);
        $mid = intval($this->request_body->content->mid);

        if(!$orderid || !$mid)
        {
            $this->send_datagrams($this->client_info['id'], array('status'=> false, 'msg'=> '提交信息有误'), $this->client_info['secret_key']);
        }

        // 获取订单信息
        $info = DB::select()->from("member_order")->where("memberid","=",$mid)->and_where("id","=",$orderid)->execute()->current();
        if (empty($info))
        {
            $this->send_datagrams($this->client_info['id'],  array('status'=> false, 'msg'=> '查找订单信息失败'), $this->client_info['secret_key'], false, "查找订单信息失败", "查找订单信息失败");
        }
        else
        {
            // 退款信息
            $info = Pay_Online_Refund::get_refund_info($info);
            if(isset($info['refund']))
            {
                $info['refund']['typeid'] = $info['typeid'];
                $info['refund']['addtime'] = date("Y-m-d H:i",$info['refund']['addtime']);
                $info['refund']['modtime'] = $info['refund']['modtime'] ? date("Y-m-d H:i",$info['refund']['modtime']) : '';
                $info['refund']['refund_proof'] = $info['refund']['refund_proof'] ? Model_Api_Standard_System::reorganized_resource_url($info['refund']['refund_proof']) : '';
            }
            $result = array('status'=> true,'msg'=> '获取成功','refund'=> isset($info['refund']) ? $info['refund'] : '');
            $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
        }
    }


    // 获取退款前置信息
    public function action_get_refund_data()
    {
        $orderid = intval($this->request_body->content->orderid);
        $mid = intval($this->request_body->content->mid);

        if(!$orderid || !$mid)
        {
            $this->send_datagrams($this->client_info['id'], array('status'=> false, 'msg'=> '提交信息有误'), $this->client_info['secret_key']);
        }

        $result = Model_Api_Standard_Refund::check_refund_info($orderid, $mid);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    // 订单退订申请
    public function action_order_refund()
    {

        $ordersn = Common::remove_xss($this->request_body->content->ordersn);
        $mid     = intval($this->request_body->content->mid);
        $typeid  = intval($this->request_body->content->typeid);

        if(!$ordersn || !$mid)
        {
            $this->send_datagrams($this->client_info['id'], array('status'=> false, 'msg'=> '提交信息有误'), $this->client_info['secret_key']);
        }

        $data['ordersn'] = $ordersn;
        $data['refund_reason'] = Common::remove_xss($this->request_body->content->refundReason);
        $data['platform'] = Common::remove_xss($this->request_body->content->refundType);
        $data['alipay_account'] = Common::remove_xss($this->request_body->content->alipay_account);
        $data['cardholder'] = Common::remove_xss($this->request_body->content->cardholder);
        $data['cardnum'] = Common::remove_xss($this->request_body->content->cardnum);
        $data['bank'] = Common::remove_xss($this->request_body->content->bank);
        // 退款处理
        $refund = Pay_Online_Refund::apply_order_refund($data,$mid,Model_Api_Standard_Pub::get_model_class_name($typeid));
        $result['status'] = $refund['status'] == 1 ? true : false;
        $result['msg'] = $refund['message'];

        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }



}